package com.example.gobang.model;

import com.example.gobang.constant.Constants;
import lombok.Data;

import java.util.UUID;

@Data
public class Room {
    private String roomId;
    private UserInfo user1;
    private UserInfo user2;
    private int whiteUser;
    private int piecesNumber;
    private int[][] board = new int[Constants.BOARD_ROW][Constants.BOARD_COL];
    public Room(){
        roomId = UUID.randomUUID().toString();

    }
//    public Room(UserInfo user1, UserInfo user2) {
//        this.user1 = user1;
//        this.user2 = user2;
//        roomId = UUID.randomUUID().toString();
//        // 默认 玩家1 为先手
//        whiteUser = user1.getUserId();
//    }

    // 判断当前位置是否已经有子
    public boolean isEmpty(int row, int col) {
        return board[row][col] == 0;
    }

    // 在当前位置落子
    public void putChess(int row, int col, int chess) {
        board[row][col] = chess;
        piecesNumber++;
    }

    // 打印棋盘
    public void printBoard() {
        System.out.println("------------------棋盘-----------------------");
        for (int i = 0; i < Constants.BOARD_ROW; i++) {
            for (int j = 0; j < Constants.BOARD_COL; j++) {
                System.out.print(board[i][j] + " ");
            }
            System.out.println();
        }
        System.out.println("------------------棋盘-----------------------");
    }

    /**
     * 判断胜负
     * @param row
     * @param col
     * @param chess
     * @return 0：尚未分出胜负 -1：平局
     */
    public int checkWinner(int row, int col, int chess) {
        if(piecesNumber == Constants.BOARD_ROW * Constants.BOARD_COL) {
            return -1;
        }
        // 行
        for (int c = 0; c < col - 4; c++) {
            try {
                if(board[row][c] == chess
                        && board[row][c + 1] == chess
                        && board[row][c + 2] == chess
                        && board[row][c + 3] == chess
                        && board[row][c + 4] == chess) {
                    return chess == user1.getUserId() ? user1.getUserId() : user2.getUserId();
                }
            } catch (ArrayIndexOutOfBoundsException e) {
                continue;
            }
        }
        // 列
        for (int r = row - 4; r <= row; r++) {
            try {
                if(board[r][col] == chess
                        && board[r + 1][col] == chess
                        && board[r + 2][col] == chess
                        && board[r + 3][col] == chess
                        && board[r + 4][col] == chess) {
                    return chess == 1 ? user1.getUserId() : user2.getUserId();
                }
            } catch (ArrayIndexOutOfBoundsException e) {
                // 若出现数据下标越界的情况，则继续判断
                continue;
            }
        }
        // 左对角线
        for (int r = row - 4, c = col - 4; r <= row && c <= col; r++, c++) {
            try {
                if(board[r][c] == chess
                        && board[r + 1][c + 1] == chess
                        && board[r + 2][c + 2] == chess
                        && board[r + 3][c + 3] == chess
                        && board[r + 4][c + 4] == chess) {
                    return chess == 1 ? user1.getUserId() : user2.getUserId();
                }
            } catch (ArrayIndexOutOfBoundsException e) {
                continue;
            }
        }
        // 右对角线
        for (int r = row - 4, c = col + 4; r <= row && c >= col; r++, c--) {
            try {
                if(board[r][c] == chess
                        && board[r + 1][c - 1] == chess
                        && board[r + 2][c - 2] == chess
                        && board[r + 3][c - 3] == chess
                        && board[r + 4][c - 4] == chess) {
                    return chess == 1 ? user1.getUserId() : user2.getUserId();
                }
            } catch (ArrayIndexOutOfBoundsException e) {
                continue;
            }
        }
        return 0;
    }
}
